﻿using DDDSample.Domain.NHibernate.Mappings;
using DDDSample.UI.Mvc.Annotations;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;
using NHibernate.Tool.hbm2ddl;

namespace DDDSample.UI.Mvc
{
    internal class NHibernateHelper
    {
        internal static ISessionFactory SessionFactory { get; [UsedImplicitly] private set; }

        internal static ISessionFactory BuildSessionFactory(bool buildTables = false)
        {
            return Fluently.Configure()
                .Database(SQLiteConfiguration.Standard.ShowSql().ConnectionString(c => c.FromConnectionStringWithKey("ddd")))
                .CurrentSessionContext("web")
                .Mappings(m => m.FluentMappings.AddFromAssemblyOf<CargoMap>()).ExposeConfiguration(c =>
                {
                    //c.SetInterceptor(new SqlInterceptor());
                    if (buildTables)
                    {
                        var schema = new SchemaExport(c);
                        schema.Execute(false, true, false);
                    }
                }).BuildSessionFactory();
        }
    }
}